package com.haierCamera.customapplication.ui.main;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.NetworkSpecifier;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
//import android.net.wifi.WifiNetworkSpecifier;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PatternMatcher;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.content.IntentFilter;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklActivitySoftApAddBinding;
import com.haierCamera.customapplication.ui.business.Business;
import com.haierCamera.customapplication.ui.business.util.WifiManagerUtil;
import com.haierCamera.customapplication.ui.main.utils.WifiAutoConnectManager;
import com.haierCamera.customapplication.ui.main.utils.WifiUtils;
import com.haierCamera.customapplication.ui.main.widget.DialogLinkDeviceWifiNotify;
import com.haierCamera.customapplication.ui.main.widget.PasswordDialog;
import com.haierCamera.customapplication.ui.main.widget.PasswordSetListener;
import com.lechange.common.log.Logger;
import com.lechange.opensdk.softap.LCOpenSDK_SoftAPConfig;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;

public class HZKLSoftAPAddActivity extends BaseActivity implements Handler.Callback,PasswordSetListener, Runnable {

    HzklActivitySoftApAddBinding binding;

//    ConnectAsyncTask mConnectAsyncTask = null;

    public String ssid = "";
    WifiAutoConnectManager.WifiCipherType type = WifiAutoConnectManager.WifiCipherType.WIFICIPHER_NOPASS;
    public static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
    public static String password = "";//"hyc888888";

    private String devId;
    private String key = "";
    /**
     * 处理信号量改变或者扫描结果改变的广播
     */

    private IntentFilter mWifiSearchIntentFilter;
    private BroadcastReceiver mWifiConnectBroadcastReceiver;
    private IntentFilter mWifiConnectIntentFilter;
//    private WifiAutoConnectManager mWifiAutoConnectManager;

    boolean isLinked = false;

    private WifiManagerUtil managerUtil;
    private PasswordDialog passwordDialog;
    private Handler handler;
    private boolean isCheckOnline = true;

    private final int ON_LINE_SUCCESS = 0x11;
    private final int ON_LINE_FAILED = 0x12;
    private final int CHECK_DEVICE_ONLINE = 0X1B;
    private final int ADD_DEVICE_SUCCESS = 0x13;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_soft_ap_add);

        binding.imgReturn.setOnClickListener((v)->{
            finish();
        });

        if (getIntent().getStringExtra("dt").contains("TF1D")) {
            binding.ivBg.setImageResource(R.drawable.hzkl_tf1d_ap_intro);
        } else if (getIntent().getStringExtra("dt").contains("TE3")) {
            binding.ivBg.setImageResource(R.drawable.hzkl_te3_ap_intro);
        } else if (getIntent().getStringExtra("dt").contains("TF1T")) {
            binding.ivBg.setImageResource(R.drawable.hzkl_tf1t_ap_intro);
        } else if (getIntent().getStringExtra("dt").contains("TP7S")) {
            binding.ivBg.setImageResource(R.drawable.hzkl_tp7se_ap_intro);
        } else if (getIntent().getStringExtra("dt").contains("TP2E")) {
            binding.ivBg.setImageResource(R.drawable.hzkl_tp2e_ap_intro);
        } else if (getIntent().getStringExtra("dt").contains("TC2E")) {
            binding.ivBg.setImageResource(R.drawable.hzkl_tc2e_ap_intro);
        } else if (getIntent().getStringExtra("dt").contains("TF3")) {
            binding.ivBg.setImageResource(R.drawable.hzkl_tf3_ap_intro);
        } else {
            binding.ivBg.setImageResource(R.drawable.hzkl_tp2e_ap_intro);
        }

        ssid = getIntent().getStringExtra("dt") + "-"
                + getIntent().getStringExtra("sn")
                .substring(getIntent().getStringExtra("sn").length()-4,getIntent().getStringExtra("sn").length());
        password = getIntent().getStringExtra("sc");
        devId = getIntent().getStringExtra("sn");

        Log.i("TAG","ssid-" + ssid);
        Log.i("TAG","sc-" + password);
        initWifiSate();

        managerUtil = new WifiManagerUtil(getApplicationContext());
        passwordDialog = new PasswordDialog();
        passwordDialog.setListener(this);
        handler = new Handler(this);
        processWithPermission(()->{
//            connectWifi();
//            WifiUtils.getInstance(this).connectWifiPws(ssid,password);
        }, Manifest.permission.ACCESS_COARSE_LOCATION);
        processWithPermission(()->{
        }, Manifest.permission.ACCESS_FINE_LOCATION);
        processWithPermission(()->{
        }, Manifest.permission.ACCESS_WIFI_STATE);
        processWithPermission(()->{
        }, Manifest.permission.CHANGE_WIFI_STATE);

        if (managerUtil.getCurrentWifiInfo().getSSID().substring(1,managerUtil.getCurrentWifiInfo().getSSID().length()-1).equals(ssid)) {
            Log.i("TAG","onCreate-searchDevice");
            searchDevice();
        } else {
        //    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) {
                showConnectDialog();
        //    } else {
//                setText(ssid
//                        ,getIntent().getStringExtra("sc"));
//                binding.btnAdd.setVisibility(View.GONE);
//            }
        }

        binding.btnAdd.setOnClickListener((v)->{
            Intent intent = new Intent(HZKLSoftAPAddActivity.this, InitDeviceActivity.class);
            intent.putExtra("devSc", getIntent().getStringExtra("sc"));//携带设备SC码
            intent.putExtra("devId", getIntent().getStringExtra("sn"));
            startActivityForResult(intent,10000);
            finish();

        });
    }

    private void showConnectDialog(){
        DialogLinkDeviceWifiNotify dialogLink = new DialogLinkDeviceWifiNotify(this);
        dialogLink.setTitle("“四川广电”想要加入无线局域网" + getIntent().getStringExtra("dt")
                + "-" + getIntent().getStringExtra("sn")
                .substring(getIntent().getStringExtra("sn").length()-4,getIntent().getStringExtra("sn").length())
                +"吗？");
        dialogLink.setCancel("取消", new DialogLinkDeviceWifiNotify.IOnCancelListener() {
            @Override
            public void onCancel(DialogLinkDeviceWifiNotify dialog) {
                dialog.dismiss();
                setText(ssid
                        ,getIntent().getStringExtra("sc"));
                binding.btnAdd.setVisibility(View.GONE);
            }
        });
        dialogLink.setConfirm("确认", new DialogLinkDeviceWifiNotify.IOnConfirmListener() {
            @Override
            public void onConfirm(DialogLinkDeviceWifiNotify dialog) {

//                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
                    setText(ssid
                            ,getIntent().getStringExtra("sc"));
//                } else {
//
//                    binding.tvIntro.setText("正尝试自动切换Wi-Fi中...\nWi-Fi如长时间不切换，您也可以手动切换。");
//                }

                wifiLink(ssid,password);

            }
        });
        dialogLink.show();
    }

//    Runnable checkRunnable = new Runnable() {
//        @Override
//        public void run() {
//            //30秒后移除在线检查,并且返回失败，正常情况第一次就可以查询到在线的状态
//            handler.removeCallbacksAndMessages(CHECK_DEVICE_ONLINE);
//            handler.obtainMessage(ON_LINE_FAILED).sendToTarget();
//            isCheckOnline = false;
//           Log.e("TAG", "check online timeout");
//        }
//    };

    public void wifiLink(String ssid,String sc) {
        Log.i("TAG","wifiLink-" + ssid + "/-" + sc);
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q)
//        {
//            NetworkSpecifier specifier =
//                    new WifiNetworkSpecifier.Builder()
//                            .setSsidPattern(new PatternMatcher(ssid, PatternMatcher.PATTERN_PREFIX))
//                            .setWpa2Passphrase(sc)
//                            .build();
//
//            NetworkRequest request =
//                    new NetworkRequest.Builder()
////                            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
//                            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
////                            .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
//                            .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
//                            .setNetworkSpecifier(specifier)
//                            .build();
//
//            ConnectivityManager connectivityManager = (ConnectivityManager)
//                    getSystemService(Context.CONNECTIVITY_SERVICE);
//
//            ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
//                @Override
//                public void onAvailable(Network network) {
//                    // do success processing here..
//                }
//
//                @Override
//                public void onUnavailable() {
//                    // do failure processing here..
//                }
//            };
//            connectivityManager.requestNetwork(request, networkCallback);
//            // Release the request when done.
//            // connectivityManager.unregisterNetworkCallback(networkCallback);
//        } else {

            managerUtil.connectWifi(ssid, sc);
//        }

        new Handler().postDelayed(()->{
            if (!managerUtil.getCurrentWifiInfo().getSSID().substring(1,managerUtil.getCurrentWifiInfo().getSSID().length()-1).equals(ssid)) {
                setText(ssid
                        ,getIntent().getStringExtra("sc"));
                binding.btnAdd.setVisibility(View.GONE);

            }
        },5000);
    }

    @Override
    public boolean handleMessage(Message msg) {
        Log.i("TAG","handleMessage-msg-" + msg.what);
        switch (msg.what) {
            case LCOpenSDK_SoftAPConfig.MSG_WHAT:
//                mProgressDialog.setStop();
//                Toast.makeText(getApplicationContext(), String.valueOf(msg.arg1), Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.HZKLswitch_wifi_and_start_check_online), Toast.LENGTH_LONG).show();
                break;
            case CHECK_DEVICE_ONLINE:
                Log.e("TAG","CHECK_DEVICE_ONLINE");
//                checkOnline();
//                checkOnBindAndRemind();
                break;
            case ON_LINE_FAILED:
//                Logger.d(TAG, "check_online_failed");
//                Toast.makeText(getApplicationContext(), "check_online_failed", Toast.LENGTH_SHORT).show();
//                handler.removeCallbacksAndMessages(CHECK_DEVICE_ONLINE);
//                mProgressDialog.setStop();
                break;
            case ON_LINE_SUCCESS:
                Log.e("TAG","ON_LINE_SUCCESS");
                //设备在线
//                Logger.d(TAG, "check_online_success");
//                handler.removeCallbacks(checkRunnable);
//                unBindDeviceInfo();
//                mProgressDialog.setStart(getResources().getString(R.string.binding_device));
                break;
            case ADD_DEVICE_SUCCESS:
//                mProgressDialog.setStop();
//                Logger.d(TAG, "add_device_success");
//                Toast.makeText(getApplicationContext(), "SuccessAddDevice", Toast.LENGTH_SHORT).show();
//                startActivity(new Intent(SoftAPActivity.this, DevicelistActivity.class));
//                finish();
                break;
        }
        return false;
    }

    @Override
    public void run() {
        //超时操作
        Toast.makeText(getApplicationContext(), getString(R.string.HZKLswitch_wifi_timeout), Toast.LENGTH_SHORT).show();
    }

    /**
     * 失败文字编辑
     * @param name
     * @param sc
     */
    private void setText(String name,String sc) {
        binding.tvIntro.setText("请将Wi-Fi切换至“" + name + "”，\n热点密码为“");

        SpannableString clickString2 = new SpannableString(sc);
        clickString2.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Log.i("TAG","sc1");
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#3366FF")); //设置颜色
            }
        }, 0, clickString2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        binding.tvIntro.append(clickString2);
        binding.tvIntro.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件

//        //先设置原始文本
//        binding.tvIntro.setText(s);
        //使用post方法，在TextView完成绘制流程后在消息队列中被调用
        binding.tvIntro.post(new Runnable() {
            @Override
            public void run() {
                //获取第一行的宽度
                float lineWidth = binding.tvIntro.getLayout().getLineWidth(0);
                //获取第一行最后一个字符的下标
                int lineEnd = binding.tvIntro.getLayout().getLineEnd(0);
                //计算每个字符占的宽度
                float widthPerChar = lineWidth / (lineEnd + 1);
                //计算TextView一行能够放下多少个字符
                int numberPerLine = (int) Math.floor(binding.tvIntro.getWidth() / widthPerChar);
                //在原始字符串中插入一个空格，插入的位置为numberPerLine - 1
                StringBuilder stringBuilder = new StringBuilder(binding.tvIntro.getText().toString())
                        .insert(numberPerLine - 1, "");

                //SpannableString的构建
                SpannableString spannableString = new SpannableString(stringBuilder.toString() + "");
                Drawable drawable = getResources().getDrawable(R.drawable.hzkl_name_copy);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
                spannableString.setSpan(new ClickableSpan() {

                    @Override
                    public void onClick(View widget) {
                        Log.i("TAG","sc2");
                        copyToBoard(sc);
                        startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(Color.parseColor("#3366FF")); //设置颜色
                    }
                }, spannableString.length() -12, spannableString.length() , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//                spannableString.setSpan(new ClickableSpan() {
//                    @Override
//                    public void onClick(View view) {
//                        //点击的响应事件
//                        Log.i("TAG","span");
//                        copyToBoard(sc);
//                        startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
//                    }
//                }, spannableString.length()-1, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//                spannableString.setSpan(imageSpan, spannableString.length() - 14, spannableString.length()-13, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                binding.tvIntro.setText(spannableString);
            }
        });
        binding.tvIntro2.setVisibility(View.GONE);

        binding.tvIntro.append("”，复制并前往Wifi列表");
      //  if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) {
            binding.tvIntro2.setVisibility(View.VISIBLE);
            binding.tvIntro2.setText("\nWi-Fi如长时间不切换，您需手动切换。");
       // }

        binding.tvIntro.setHighlightColor(getResources().getColor(android.R.color.transparent));
    }

    private void copyToBoard(String s){
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", s);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        toast("已复制到剪切板");
    }

    private void searchDevice() {
//        if (!managerUtil.isWifi(getApplicationContext())) {
//            Log.i("TAG","不是wifi");
//            Toast.makeText(getApplicationContext(), getString(R.string.HZKLconnect_device_wifi), Toast.LENGTH_SHORT).show();
//            return;
//        }
//        com.lechange.common.log.Logger.d(SoftAPActivity.this.getClass().getSimpleName(), " devHotSpot : " + devHotSpot);
//                if (isConnectedDevHot()) {
        if (null != passwordDialog && !passwordDialog.isAdded()) {
            handler.removeCallbacks(HZKLSoftAPAddActivity.this);
//            mProgressDialog.setStart(getString(R.string.search_devices));
//            binding.tvIntro.setText("搜索设备中...");
            showLoadingDialog("搜索设备中...");
            binding.btnAdd.setVisibility(View.GONE);
            Log.i("TAG","devId-" + devId);
            Business.getInstance().searchDevice(devId, 30 * 1000, new Handler() {
                public void handleMessage(final Message msg) {
                    Log.i("TAG","searchDevice-" + "(" + msg.what  + ")" + "/msg-" + msg.obj.toString());
                    dismissLoadingDialog();
                    if (msg.what == 1 || msg.what == 2 || msg.what == 0) {
                        //需要初始化
//                        passwordDialog.setCurrentType(1);
//                        passwordDialog.show(getSupportFragmentManager(), "passwordDialog");

                        Log.i("TAG","startSoftAPConfig-ssid:" +getIntent().getStringExtra("ssid")
                                + "/pwd:" + getIntent().getStringExtra("ssidPwd") + "/devId:" + devId + "/sc:"+ getIntent().getStringExtra("sc"));

                     //   LCOpenSDK_SoftAPConfig.startSoftAPConfig(ssid, pwd, devId, psw1, handler, 30 * 1000);
                        LCOpenSDK_SoftAPConfig.startSoftAPConfig(getIntent().getStringExtra("ssid")
                                , getIntent().getStringExtra("ssidPwd"), devId
                                , getIntent().getStringExtra("sc"),true, handler, 30 * 1000);
                        Business.getInstance().stopSearchDevice();

                        binding.tvIntro2.setVisibility(View.GONE);
                        binding.tvIntro.setText("请耐心等待，当听到“网络连接成功”的提示音时，点击下方添加按钮");
                        binding.btnAdd.setVisibility(View.VISIBLE);

                    }
////                    else if (msg.what == 2) {
////                        //已经初始化
////                        passwordDialog.setCurrentType(2);
////                        passwordDialog.show(getSupportFragmentManager(), "passwordDialog");
////                    }
////                    else if (msg.what == 0) {
////                        //不需要初始化
////                        passwordDialog.setCurrentType(0);
////                        passwordDialog.show(getSupportFragmentManager(), "passwordDialog");
////                    }
                    else {
                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.HZKLsearch_devices_timeout), Toast.LENGTH_LONG).show();
//                        mProgressDialog.setStop();

                    }
                }
            });

        }

    }

    private void initWifiSate() {

        mWifiSearchIntentFilter = new IntentFilter();
        mWifiSearchIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        mWifiSearchIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        mWifiSearchIntentFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);

        //wifi 状态变化接收广播
        mWifiConnectBroadcastReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                Log.i("TAG","mWifiConnectBroadcastReceiver-onReceive");
                String action = intent.getAction();
                if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                    int wifState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
                    if (wifState != WifiManager.WIFI_STATE_ENABLED) {
                        Toast.makeText(HZKLSoftAPAddActivity.this, "没有wifi", Toast.LENGTH_SHORT).show();
                    }
                } else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
                    int linkWifiResult = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 123);
                    Log.e("TAG", ssid + "linkWifiResult:" + linkWifiResult);
                    if (linkWifiResult == WifiManager.ERROR_AUTHENTICATING) {
                        Log.e("TAG", ssid + "onReceive:密码错误");
                    }
                } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                    NetworkInfo.DetailedState state = ((NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).getDetailedState();
                    setWifiState(state);
                }
            }
        };

        mWifiConnectIntentFilter = new IntentFilter();
        mWifiConnectIntentFilter.addAction(WifiManager.ACTION_PICK_WIFI_NETWORK);
        mWifiConnectIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        mWifiConnectIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        mWifiConnectIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

    }

    /**
     * 显示wifi状态
     *
     * @param state
     */
    @SuppressLint("NewApi")
    public void setWifiState(final NetworkInfo.DetailedState state) {
        if (state == NetworkInfo.DetailedState.AUTHENTICATING) {

        } else if (state == NetworkInfo.DetailedState.BLOCKED) {

        } else if (state == NetworkInfo.DetailedState.CONNECTED) {
            dismissLoadingDialog();
            Log.i("TAG","managerUtil.getCurrentWifiInfo().getSSID()-"
                    + managerUtil.getCurrentWifiInfo().getSSID());

            if (managerUtil.getCurrentWifiInfo().getSSID().contains(getIntent().getStringExtra("dt"))) {
                Log.i("TAG", "连接成功");
//                binding.tvIntro.setText("当听到网络连接成功的提示音时，点击下方添加按钮进行设备添加\n如果听到wifi密码错误提示音时，请返回上个界面修改wifi密码");
//                binding.btnAdd.setVisibility(View.VISIBLE);
                searchDevice();


            } else {
//                setText(ssid
//                        ,getIntent().getStringExtra("sc"));
//                binding.btnAdd.setVisibility(View.GONE);
            }
        } else if (state == NetworkInfo.DetailedState.CONNECTING) {
            Log.i("TAG","连接中...");
//            binding.tvIntro.setText("连接中...");
        } else if (state == NetworkInfo.DetailedState.DISCONNECTED) {
            Log.i("TAG","断开连接");
//            setText(ssid
//                    ,getIntent().getStringExtra("sc"));
//            binding.btnAdd.setVisibility(View.GONE);
        } else if (state == NetworkInfo.DetailedState.DISCONNECTING) {
            Log.i("TAG","断开连接中");

        } else if (state == NetworkInfo.DetailedState.FAILED) {

            Log.i("TAG","连接失败");
            setText(ssid
                    ,getIntent().getStringExtra("sc"));
            binding.btnAdd.setVisibility(View.GONE);
        } else if (state == NetworkInfo.DetailedState.IDLE) {

        } else if (state == NetworkInfo.DetailedState.OBTAINING_IPADDR) {

        } else if (state == NetworkInfo.DetailedState.SCANNING) {

        } else if (state == NetworkInfo.DetailedState.SUSPENDED) {

        }
    }

    @Override
    public void onSaveSuccess(String psw1) {
        key=psw1;

    }

    @Override
    public void onWifiPassWord(String psw1) {

    }

    @Override
    protected void onResume() {
        super.onResume();

        registerReceiver(mWifiConnectBroadcastReceiver, mWifiConnectIntentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();

        unregisterReceiver(mWifiConnectBroadcastReceiver);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        LCOpenSDK_SoftAPConfig.cancel();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            isCheckOnline = false;
            handler = null;
        }
    }
}
